package com.huawei.chengdu.meetingdemo.callback;

import android.widget.Toast;

import com.device.idea.meeting.Addressbook.model.AddrBookNetUserInfo;
import com.device.idea.meeting.Addressbook.model.AddrBookUserInfo;
import com.device.idea.meeting.CallCtrl.model.ConfState;
import com.device.idea.meeting.CallRecord.model.CallRecordList;
import com.device.idea.meeting.ConfCtrl.model.AttendeeInfo;
import com.device.idea.meeting.ConfCtrl.model.NonChairCapability;
import com.device.idea.meeting.ConfCtrl.model.ParticipantInfo;
import com.device.idea.meeting.ConfManage.model.BookConfInfo;
import com.device.idea.meeting.FlexibleLayout.model.FlexibleLayout;
import com.device.idea.meeting.IMeetingControlEventHandler;
import com.device.idea.meeting.MultiStream.model.LayoutInfo;
import com.device.idea.meeting.MultiStream.model.PreviewLayout;
import com.device.idea.meeting.Share.model.ShareCastInfo;
import com.device.idea.meeting.Share.model.ShareCurInfo;
import com.device.idea.meeting.Share.model.ShareWhiteBoardInfo;
import com.huawei.chengdu.meetingdemo.consts.ObserverConts;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;
import com.huawei.chengdu.meetingdemo.presenter.MeetingManager;
import com.huawei.chengdu.meetingdemo.util.LogUtil;
import com.huawei.chengdu.meetingdemo.view.HomeActivity;

import java.util.ArrayList;
import java.util.List;

public class MeetingControlEventCallback implements IMeetingControlEventHandler {
    private static final String TAG = MeetingControlEventCallback.class.getSimpleName();
    private HomeActivity uiInstance;

    public MeetingControlEventCallback(HomeActivity homeActivity) {
        uiInstance = homeActivity;
    }

    @Override
    public void onInitNotify() {
        LogUtil.info(TAG + " onInitNotify");
    }

    @Override
    public void onSpeekerStatusNotify(boolean b, int i) {

    }

    @Override
    public void onMicrophoneStatusNotify(boolean isMute) {
        LogUtil.info("Mic status is change, isMute = " + isMute);
    }

    @Override
    public void onCameraStatusNotify(boolean b) {

    }

    @Override
    public void onSipRegStatusChangeNotify(boolean isSuccess, int errCode) {
        LogUtil.info(TAG + " onSipRegStatusChangeNotify,isSuccess=" + isSuccess + ",errCode=" + errCode);
        uiInstance.runOnUiThread(()->Toast.makeText(uiInstance, isSuccess ? "注冊SIP服务器成功" : "注册SIP服务器失败 错误码 = " + errCode, Toast.LENGTH_LONG).show());
    }

    @Override
    public void onSipForceDeRegNotify() {

    }

    @Override
    public void onH323RegStatusChangeNotify(boolean isSuccess, int errCode) {
        Toast.makeText(uiInstance, isSuccess ? "注冊H323服务器成功" : "注册H323服务器失败 错误码 = " + errCode, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onAddressBookChangeNotify() {
        LogUtil.info(TAG + " onAddressBookChangeNotify");
        ConfSdkPresenter.searchAllLocalUsers(new MeetingCallbackSelf<List<AddrBookUserInfo>>(TAG + " searchAllUsers") {
            // 统一在MeetingControlEventCallback中处理，这里不重复处理
            @Override
            public void onSuccess(List<AddrBookUserInfo> addrBookUserInfos) {}
            @Override
            public void onFailed(int errCode, String errMsg) {}
        }.getMeetingCallback());
    }

    @Override
    public void onAddressBookUserResult(List<AddrBookUserInfo> list) {
        LogUtil.info("search local contact list = " + list);
        uiInstance.initLocalContact(list == null ? new ArrayList<>() : list);
        MeetingManager.getInstance().getAddressBookPresenter().onAddressBookUserResult(list);
    }

    @Override
    public void onAddressBookAllUserResult(List<AddrBookUserInfo> list) {
        LogUtil.info("All local contact list = " + list);
        uiInstance.initLocalContact(list == null ? new ArrayList<>() : list);
        MeetingManager.getInstance().getAddressBookPresenter().onAddressBookAllUserResult(list);
    }

    @Override
    public void onAddressBookNetUserResult(List<AddrBookNetUserInfo> list) {
        LogUtil.info("Get company contact list = " + list);
        uiInstance.initCompanyContact(list == null ? new ArrayList<>() : list);
        MeetingManager.getInstance().getAddressBookPresenter().onAddressBookNetUserResult(list == null ? new ArrayList<>() : list);
    }

    @Override
    public void onConfStateChangeNotify(ConfState confState) {
        LogUtil.info( "Conf state change, state = " + confState);
        uiInstance.runOnUiThread(()->uiInstance.handleConfStateChange(confState));
    }

    @Override
    public void onEndCallNotify(int callCode) {
        LogUtil.info("End call, code = " + callCode);
        uiInstance.dismissAllDialog();
    }

    @Override
    public void onNoStreamNotify(int i) {
        LogUtil.info(TAG + " onNoStreamNotify");
    }

    @Override
    public void onCallInComeNotify(String displayName, String calleeNumber) {
        uiInstance.incomingCall(displayName, calleeNumber);
    }

    @Override
    public void onAddVideoNotify(boolean isShow) {
        LogUtil.info(TAG + " onAddVideoNotify,isShow=" + isShow);
        uiInstance.observeAudioToVideo(isShow);
    }

    @Override
    public void onCallRecordRefreshNotify(CallRecordList callRecordList) {
        LogUtil.info("Call record list refresh, " +
                "accept size = " + callRecordList.getCallAcceptRecord().size() +
                " no ans size = " + callRecordList.getCallNoAnsRecord().size() +
                " call out size = " + callRecordList.getCallOutRecord().size());
        uiInstance.initCallRecord(callRecordList);
    }

    @Override
    public void onConfctrlJoinedConferenceNotify(int userId) {
    }

    @Override
    public void onConfctrlTerminalLeftConferenceNotify(int userId) {
    }

    @Override
    public void onLocalBroadcastNotify(boolean b, int i) {

    }

    @Override
    public void onBroadcastedAttendeeNotify(int i, boolean b) {

    }

    @Override
    public void onUserJoinNotify(AttendeeInfo attendeeInfo) {

    }

    @Override
    public void onUserLeftNotify(int i, AttendeeInfo attendeeInfo) {

    }

    @Override
    public void onLockConfNotify(boolean b) {

    }

    @Override
    public void onConfRemainTimeNotify(int i) {

    }

    @Override
    public void onConfctrlTerminalYouAreSeeingNotify(int i) {

    }

    @Override
    public void onConfctrlMuteMultisiteNotify(boolean b) {

    }

    @Override
    public void onConfctrlSpeakerMultisiteNotify(boolean b) {

    }

    @Override
    public void onFloorAttendeeNotify(List<Integer> list) {

    }

    @Override
    public void onVoiceCtrlSwitchNotify(boolean b) {

    }

    @Override
    public void onConfctrlUpdateSiteListNotify(List<ParticipantInfo> list) {
        LogUtil.info(TAG + " onConfctrlUpdateSiteListNotify,list=" + list);
        MeetingManager.getInstance().onConfctrlUpdateSiteListNotify(list);
    }

    @Override
    public void onConfctrlOrderConfListNotify(List<BookConfInfo> list) {
        LogUtil.info("Receive book conf list = " + list);
        if (list == null) return;
        uiInstance.initBookConfInfo(list);
    }

    @Override
    public void onLayoutInfoNotify(LayoutInfo layoutInfo) {
        LogUtil.info("Receive LayoutInfo = " + layoutInfo);
        if (layoutInfo != null) {
            uiInstance.handleLayoutChangeNotify(layoutInfo);
        }
    }

    @Override
    public void onShareNotify(ShareCurInfo shareCurInfo) {
        LogUtil.info("on share notify = " + shareCurInfo);
        uiInstance.handleShareStateChange(shareCurInfo);
    }

    @Override
    public void onShareWhiteboardNotify(ShareWhiteBoardInfo shareWhiteBoardInfo) {
        LogUtil.info("onShareWhiteboardNotify: shareWhiteBoardInfo=" + shareWhiteBoardInfo);
    }

    @Override
    public void onLocalChairReleasedNotify() {
        LogUtil.info("onLocalChairReleasedNotify");
        MeetingManager.getInstance().noticeDataChange(ObserverConts.LOCAL_CHAIR_RELEASED_MSG, null);
    }

    @Override
    public void onCastPortNotify(ShareCastInfo shareCastInfo) {

    }

    @Override
    public void onAddLdapToLocal(AddrBookUserInfo addrBookUserInfo) {

    }

    @Override
    public void onDisturbStateChangeNotify() {

    }

    @Override
    public void onPreviewStatusChangeNotify(PreviewLayout previewLayout) {

    }

    @Override
    public void onMicMuteDetectNotify(boolean isMute) {}

    @Override
    public void disableAudioTips() {

    }

    @Override
    public void onFlexibleLayoutNotify(List<FlexibleLayout> list) {
        LogUtil.info("receive flexible layout = ");
        for (FlexibleLayout flexibleLayout : list) {
            LogUtil.info(flexibleLayout.toString());
        }
        uiInstance.handleFlexibleLayoutChangeNotify(list);
    }

    @Override
    public void onShareAuxTokenNotify() {

    }

    @Override
    public void onStartShareNotify(int a) {

    }

    @Override
    public void onAudioNotify(int a) {

    }

    @Override
    public void onNonChairCtrlCapsNotify(NonChairCapability nonChairCapability) {}

    @Override
    public void onMediaCtrDeadNotify() {

    }

}
